

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Data_Device extends JFrame{
    public Data_Device(String idDevice){

        SQLOperation sql=new SQLOperation();

        String[] dataBelongs={"Lamps","WindowsDoors","Humidity","Temperature"};
        String[][] dataInfo={{"isOn","timeStamp"},
                {"isOpen","timeStamp"},
                {"hygro","timeStamp"},
                {"tempNum","timeStamp"}};
        int num=-1;
        Object[][] detailData;
        for(int i=0;i<4;i++){

            detailData=sql.search(dataBelongs[i],dataInfo[i], "Device_idDevice",idDevice);

            if(detailData.length!=0){
                    num=i;
                    break;

            }
        }
        if(num!=-1) {

            setTitle("Information");
            Container container = getContentPane();
            setLayout(new BorderLayout());

            JPanel titlePanel = new JPanel();

            JLabel title = new JLabel("Device Data of '" + idDevice + "'");
            title.setFont(new Font("", Font.BOLD, 20));

            titlePanel.add(title,BorderLayout.NORTH);

            Object[][] listData = sql.search(dataBelongs[num], dataInfo[num], "Device_idDevice", idDevice);

            String[] curAttribute=new String[dataInfo[num].length];
            for(int i=0;i<dataInfo[num].length;i++) curAttribute[i]=String.valueOf(dataInfo[num][i]);



            JTable dataList = new JTable(listData, dataInfo[num]);

            dataList.setRowHeight(30);
            dataList.setFont(new Font("", Font.PLAIN, 15));

            dataList.setEnabled(true);
            dataList.getTableHeader().setFont(new Font("", Font.BOLD, 20));
            JScrollPane scrollPane = new JScrollPane(dataList);

            add(scrollPane, BorderLayout.CENTER);

            JButton back = new JButton("Back");
            back.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }
            });
            JComboBox selectCombo = new JComboBox();
            JButton further = new JButton("Analyze");

            if (num == 0 || num == 1) {
                selectCombo.addItem("off");
                selectCombo.addItem("on");

            } else {

                selectCombo.addItem("Less than:");
                selectCombo.addItem("More than:");


            }

            JTextField textBox = new JTextField(8);
            if (num == 0 || num == 1) {
                textBox.setEnabled(false);
            } else {
                textBox.setEnabled(true);
                textBox.setText("40");
            }
            JButton visual=new JButton("Visualize");


            further.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!textBox.isEnabled()){
                        new DataFinalList(listData,curAttribute, selectCombo.getSelectedIndex() == 1,0);
                    }
                    else{
                        new DataFinalList(listData,curAttribute, selectCombo.getSelectedIndex() == 1,
                                Integer.parseInt(textBox.getText()),0);
                    }
                }
            });
            JPanel buttons=new JPanel();
            buttons.add(back);
            buttons.add(visual);
            buttons.add(selectCombo);
            buttons.add(textBox);
            buttons.add(further);
            add(buttons,BorderLayout.SOUTH);

            setSize(600,500);
            setResizable(false);
            setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
            setVisible(true);

        }
        else{
            JOptionPane.showMessageDialog
                    (null, "Invalid idDevice!","Empty select",JOptionPane.ERROR_MESSAGE);
            setVisible(false);


        }







    }
}